﻿using MediatR;
using PublicCommon;
using T_Person.BaseRep.Interface;
using T_Person.Domain;
using T_Person.Read.APi.Applaction.Command.Employment;

namespace T_Person.Read.APi.Applaction.CommandHandlers.PersonnelHandlers
{
    public class EmployeementSearchByaApprovelHandler : IRequestHandler<EmployeementByApprovalCommand, PageResult<Personnel>>
    {
        private readonly  IPersonnelRep personnelRep;

        public EmployeementSearchByaApprovelHandler(IPersonnelRep personnelRep)
        {
            this.personnelRep = personnelRep;
        }

        public async Task<PageResult<Personnel>> Handle(EmployeementByApprovalCommand request, CancellationToken cancellationToken)
        {
            var list = await personnelRep.GetvaluesByResult(x => x.IsDel == false && x.RLZId.Equals(1)&&x.AuditState>0);

            if (!string.IsNullOrEmpty(request.name))
            {
                list = list.Where(x => x.Dname == request.dname);
            };
            if (!string.IsNullOrEmpty(request.rname))
            {
                list = list.Where(x => x.Rname == request.rname);
            }
            if (!string.IsNullOrEmpty(request.dname))
            {
                list = list.Where(x => x.Dname == request.dname);
            }
            if (request.shengpi >0)
            {
                list = list.Where(x => x.AuditState.Equals(request.shengpi));
            }
            if (!string.IsNullOrEmpty(request.daogangriqi.ToString()))
            {
                list = list.Where(x => x.EntryDate == request.daogangriqi);
            }
            if (!string.IsNullOrEmpty(request.qianshuriq.ToString()))
            {
                list = list.Where(x => x.CreateDate == request.qianshuriq);
            }
            int listcount = list.Count();
            int pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pagesize);
            list = list.OrderByDescending(x => x.Id).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize);
            return new PageResult<Personnel>
            {
                List = list.ToList(),
                PageCount = pagecount,
                TotalCount = listcount,
            };
        }
    }
}
